import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:mall/constant/app_colors.dart';
import 'package:mall/constant/app_strings.dart';
import 'package:mall/gen/assets.gen.dart';
import 'package:mall/gen/r.dart';
import 'package:mall/modules/model/home_entity.dart';
import 'package:mall/modules/sc/home_sc.dart';
import 'package:mall/router/navigator_util.dart';
import 'package:mall/ui/page/home/tabhome/tab_home_banner.dart';
import 'package:mall/ui/page/home/tabhome/tab_home_brand.dart';
import 'package:mall/ui/page/home/tabhome/tab_home_coupon.dart';
import 'package:mall/ui/page/home/tabhome/tab_home_goods.dart';
import 'package:mall/ui/page/home/tabhome/tab_home_goods_category.dart';
import 'package:mall/ui/page/home/tabhome/tab_home_group_by.dart';
import 'package:mall/ui/page/home/tabhome/tab_home_special.dart';
import 'package:mall/utils/image_component/image_component.dart';
import 'package:mall/utils/list_placeholder_component/list_placeholder_component.dart';
import 'package:mall/utils/refresh/refresh_widget.dart';
import 'package:mall/utils/refresh/warp_refresh_controller.dart';

class TabHomePage extends StatefulWidget {
  @override
  _TabHomePageState createState() => _TabHomePageState();
}

class _TabHomePageState extends State<TabHomePage> with SingleTickerProviderStateMixin {
  final sc_home = Get.put(HomeSc());
  // 刷新组件控制器
  late final WarpRefreshController _refreshController = WarpRefreshController(loadState: sc_home.loadState);

  // TabHomeViewModel _model = TabHomeViewModel();
  // VoidCallback callback;
  // RefreshController _refreshController;

  // @override
  // void initState() {
  //   super.initState();
  //   _refreshController = RefreshController();
  //   _onRefresh(); //获取首页数据
  // }
  @override
  void dispose() {
    super.dispose();
    _refreshController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          ImageComponent(
            Assets.imgs.homeTopBack.path,
            width: double.infinity,
            height: 175.w,
          ),
          SafeArea(
            child: Column(
              children: [
                //搜索框
                appBarWidget(),
                SizedBox(height: 10),
                Expanded(
                  child: Padding(
                    padding: const EdgeInsets.only(left: 15, right: 15),
                    child: Obx(() => RefreshComponent(
                          controller: _refreshController,
                          onRefresh: () => sc_home.refreshData(),
                          child: WrappedListPlaceholderComponent.widget1(
                            state: sc_home.loadState.value,
                            data: sc_home.data.value,
                            builder: (p0) => _dataView(p0),
                          ),
                        )),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  //appbar
  Widget appBarWidget() {
    // return SliverAppBar(
    //     floating: true,
    //     pinned: true,
    //     actions: [
    //       IconButton(
    //         icon: Icon(
    //           Icons.search,
    //           color: Colors.white,
    //         ),
    //         onPressed: () => NavigatorUtil.goSearchGoods(context),
    //       )
    //     ],
    //     flexibleSpace: FlexibleSpaceBar(
    //       centerTitle: true,
    //       title: Text(AppStrings.APP_NAME),
    //     ));
    return InkWell(
        onTap: () => NavigatorUtil.goSearchGoods(context),
        child: Container(
          padding: EdgeInsets.only(left: 15, right: 15),
          height: 34,
          child: Row(
            children: [
              //搜索框
              Expanded(
                child: Container(
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(17),
                    color: Colors.white.withOpacity(0.2),
                  ),
                  alignment: Alignment.centerLeft,
                  child: Row(
                    children: [
                      SizedBox(width: 20),
                      ImageComponent(
                        R.image.homeSearch.path,
                        size: 15,
                      ),
                      SizedBox(width: 5),
                      Text(
                        '真人娃娃',
                        style: TextStyle(fontSize: 12, color: AppColors.C_FDB8BE),
                      ),
                    ],
                  ),
                ),
              ),
              SizedBox(width: 10),
              Text('搜索', style: TextStyle(fontSize: 15, color: AppColors.C_FFFFFF)),
            ],
          ),
        ));
  }

  //数据显示
  Widget _dataView(HomeEntity _model) {
    return SingleChildScrollView(
        physics: ClampingScrollPhysics(),
        child: Column(
          children: [
            SizedBox(height: 10),
            TabHomeBanner(_model.banner ?? []),
            SizedBox(height: 15),
            TabHomeGoodsCategory(_model.channel ?? []),
            TabHomeCoupon(_model.couponList ?? []),
            TabHomeGroupByWidget(_model.grouponList ?? []),
            TabHomeGoods(AppStrings.NEW_PRODUCTS, _model.newGoodsList ?? []),
            TabHomeGoods(AppStrings.HOT_SALE, _model.hotGoodsList ?? []),
            TabHomeGoods(AppStrings.AT_HOME, _model.floorGoodsList?[0].goodsList ?? []),
            TabHomeGoods(AppStrings.KITCHEN, _model.floorGoodsList?[1].goodsList ?? []),
            TabHomeGoods(AppStrings.DIET, _model.floorGoodsList?[2].goodsList ?? []),
            TabHomeGoods(AppStrings.PARTS, _model.floorGoodsList?[3].goodsList ?? []),
            TabHomeSpecial(AppStrings.SPECIAL, _model.topicList ?? []),
            TabHomeBrand(AppStrings.MANUFACTURING, _model.brandList ?? [])
          ],
        ));
  }
}
